﻿#pragma once

#include "common.h"
#include "ui/ui.h"

namespace anip
{
	class Texture2D;

	namespace ui
	{
		class Panel;

		class TabControl : public Control
		{
		private:
			class HeaderButton;

		public:
			class Page
			{
				friend class TabControl;

			private:
				Control& _content;
				HeaderButton* _btn;

			public:
				wstring text;
				Texture2D *texture;
				bool showDelete = false;

				Control& getContent() const { return _content; }

			private:
				Page(Page&) = default;
				Page(Control& ctrl, const wstring& txt = L"", Texture2D* tex = nullptr) :
					_content(ctrl), text(txt), texture(tex) {}
			};

		private:
			Panel* _header, * _body;
			std::vector<Page*> _pages;
			int _select = -1;

			void _makeHeader();
			HeaderButton* _createButton(Page* p);

		public:
			TabControl(Container* parent,
				invec2 pos = { 0, 0 }, invec2 size = { 100, 100 });

			const std::vector<Page*>& pages() const { return _pages; }

			Page& addPage(Control& ctrl, const wstring& text, Texture2D* tex = nullptr);
			void deletePage(Page* page);
			void detachPage(Page* page);
			void reorderPage(Page* page, int n);

			void select(Page* page);

			int selectedIndex() const { return _select; }
			Page* selectedPage() const { return (_select == -1) ? nullptr : _pages[_select]; }

			void paint() override;
			void flowChildren(Flow& f) override;
		};
	}
}